%{
#include <iostream>
#include "prot.h"
#include "creator.h"
#include "y.tab.hpp"
#include "maps.h"

void unknownOn(bool cond, const char *smth) {
  if (cond) {
    std::cerr << "Lexer error at line " << yylineno << "\n";
    std::cerr << "Unknown " << smth << ": '" << yytext << "'\n";
    exit(1);
  }
}

%}
%option noyywrap
/* %option debug */
operator [.\->!,|()]
%%

\/\/[^\n]*\n ;
@flags: yylval.number = OPT_FLAGS; return OPTION;
@mode:  yylval.number = OPT_MODE;  return OPTION;
64bit   yylval.number = PM_64; 	   return PAGING_MODE; 
32bit   yylval.number = PM_32;     return PAGING_MODE;
Pae     yylval.number = PM_PAE;    return PAGING_MODE;

[0-9]+  yylval.number = atoi(yytext);     return NUMBER;
[a-z_][a-zA-Z_0-9]*  yylval.cstr = strndup(yytext, yyleng); return NAME;

{operator}  return yytext[0];
[\ \t]+  ;

\n[\ \t\r\n]* return ENDL;

#PF     return PAGE_FAULT;
#INT    return INTERRUPT;
OK      return OK;

[A-Z][A-Z_0-9]* {
    yylval.flag = flagsMap(yytext);
    unknownOn(yylval.flag == NameMap::notFound, "flag");
    return FLAG;
  }

:4kb    yylval.offset = OFFSET_MASK_4KB;   return SIZE;
:2mb    yylval.offset = OFFSET_MASK_2MB;   return SIZE;
:1gb    yylval.offset = OFFSET_MASK_1GB;   return SIZE;





Read    yylval.number = CMD_READ;    return CMD_ID;
Write   yylval.number = CMD_WRITE;   return CMD_ID;
Run     yylval.number = CMD_RUN;     return CMD_ID;
Inv     yylval.number = CMD_INV;     return CMD_ID;
Flags   yylval.number = CMD_FLAGS;   return CMD_ID;
Map     yylval.number = CMD_MAP;     return CMD_ID;
Flush   yylval.number = CMD_FLUSH;   return CMD_ID;

Result  return CMD_RES;

%%